package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/23 11:19
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 通过条件查询
     * @return
     */
    @PostMapping("getByConditions")
    public Result getByConditions(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageInfo pageInfo = permissionService.getByConditions(queryPageBean);
            return new Result(true, "查询菜单成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "查询菜单失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true,"菜单添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单添加失败");
        }
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        try {
            permissionService.delete(id);
            return new Result(true,"菜单删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单删除失败");
        }
    }

    //进行数据回显
    @GetMapping("toEdit")
    public Result toEdit(Integer id){
        //调用service传入参数
        try {
            Permission permission = permissionService.getById(id);
            return new Result(true,"查询菜单成功",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody Permission permission){
        //调用service传入参数
        try {
            permissionService.update(permission);
            return new Result(true,"菜单更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单更新失败");
        }
    }

}
